<template>
  <div class="department-container">
    <el-tree :data="data" :props="defaultProps" default-expand-all>
      <!-- <template v-slot="slotscope"> -->
      <!-- <template slot-scope="slotscope"> -->
      <template v-slot="{ data }">
        <el-row style="width: 100%">
          <el-col :span="20"><div @click="test(data)">{{ data.name }}</div></el-col>
          <el-col :span="4">
            <div class="rigth-ele">
              <span class="re-name">{{ data.managerName }}</span>
              <el-dropdown class="re-drop" @command="dropdownTmp($event,data.id)">
                <span class="el-dropdown-link">
                  操作<i class="el-icon-arrow-down el-icon--right" />
                </span>
                <el-dropdown-menu slot="dropdown">
                  <!-- 正常思维来说，点击事件，是给下面3个div分别绑定一个 @click事件 -->
                  <!-- 我在工作里面，有了一些优化的手段，减少事件的绑定 -->
                  <el-dropdown-item command="add">添加子部门</el-dropdown-item>
                  <el-dropdown-item command="edit">编辑</el-dropdown-item>
                  <el-dropdown-item command="delete">删除</el-dropdown-item>
                </el-dropdown-menu>
              </el-dropdown>
            </div>
          </el-col>
        </el-row>
      </template>
    </el-tree>
    <!-- 添加部门弹窗 Pop-ups-->
    <Addeditdialog
      ref="dialogRef"
      :is-edit.sync="isEdit"
      :show-dialog.sync="showDialog"
      :department-list="departmentList"
      :current-id="currentId"
      @add-success="companyDepartment"
    />
  </div>
</template>

<script>
import { companyDepartmentRequest, deleteADepartment } from '@/api/department'
import { transformListToTree } from '@/utils'
import Addeditdialog from './components/addeditdialog.vue'
export default {
  name: 'Department',
  components: {
    Addeditdialog
  },
  data() {
    return {
      data: [],
      departmentList: [],
      defaultProps: {
        children: 'children',
        label: 'label'
      },
      currentId: '',
      showDialog: false,
      isEdit: false
    }
  },
  created() {
    this.companyDepartment()
  },
  methods: {
    async companyDepartment() {
      // 发送axios数据
      const res = await companyDepartmentRequest()
      // 下面这个数据是平铺数据
      this.departmentList = res.data
      // 下面这个数据是 符合tree组件的  树形结构数据
      // 递归,实现树状图
      this.data = transformListToTree(res.data, 0)
    },
    dropdownTmp(type, id) {
      // 传值给pid
      this.currentId = id
      // 添加
      if (type === 'add') {
        // 新增isEdit为true就弹出新增
        this.isEdit = false
        this.showDialog = true
      } else if (type === 'delete') {
        // 删除
        this.$confirm('请确认是否要删除这个部门?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(async() => {
          await deleteADepartment(id)
          // 删除成功以后，要刷新部门列表（这个刷新就是调用接口，不是浏览器的刷新）
          this.companyDepartment()
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
      } else {
        // 编辑
        // 编辑isEdit为true就弹出编辑
        this.isEdit = true
        this.showDialog = true
      }
    },
    test(a) {
      console.log(a)
    }
  }
}
</script>

<style lang="scss" scoped>
.department-container {
  margin: 30px 50px;
  .rigth-ele {
    .re-name {
      margin-right: 10px;
    }
  }
}
</style>
